import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { StoreService, queryCondition } from '../store.service';
import { Statistic, StatisticTerm, CalculateTerm } from '../classes';
import { DataService } from '../data.service';

@Component({
  selector: 'app-statistic',
  templateUrl: './statistic.component.html',
  styleUrls: ['./statistic.component.less'],
})
export class StatisticComponent implements OnInit {
  constructor(public store: StoreService, public dataService: DataService) {}

  ngOnInit() {}
  @Input() parentCollection: Statistic[];
  @Input() current: Statistic;
  @Output('oncurrentChanged') currentChanged: EventEmitter<
    Statistic
  > = new EventEmitter<Statistic>();
  public oncurrentChanged() {
    this.currentChanged.emit(this.current);
  }
  showlist: boolean = false;
  addStatistic() {
    this.showlist = !this.showlist;
    this.selectConditions = [];
    this.dataService.clearSelection();
    // this.store.conditionlist.forEach(v => {
    //   v.selected = false;
    // });
  }
  addCalculate() {
    if (this.current.caculateterms === undefined) {
      console.log('undefined');
      this.current.caculateterms = [];
    }
    this.current.caculateterms.push(new CalculateTerm());
    // this.showlist = !this.showlist;
    // this.selectConditions = [];
    // this.dataService.clearSelection();
    // this.store.conditionlist.forEach(v => {
    //   v.selected = false;
    // });
  }
  selectConditions = [];
  changecondition($event) {
    let v = $event.target.value;
    if (this.selectConditions.includes(v))
      this.selectConditions.splice(this.selectConditions.indexOf(v), 1);
    else this.selectConditions.push(v);
  }
  selctItems() {
    if (this.selectConditions !== undefined) {
      this.selectConditions.forEach((v) => {
        let c = new StatisticTerm();
        c.target = v;
        let info = this.dataService.getInfoByTarget(v);
        c.show = info.show;
        c.name = c.show;
        c.datatype = info.datatype;
        // c.show = s.show
        this.current.terms.push(c);
      });
    }
    this.selectConditions = [];
    this.showlist = false;
  }
  cancelItems() {
    this.selectConditions = [];
    this.showlist = false;
  }
  remove(item) {
    this.current.terms.splice(this.current.terms.indexOf(item), 1);
  }
  removecaculate(item) {
    this.current.caculateterms.splice(
      this.current.caculateterms.indexOf(item),
      1
    );
  }
  select(item) {
    let c = new StatisticTerm();
    c.target = item;
    this.current.terms.push(c);
    this.showlist = false;
  }
}
